package view 
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	import model.GameData;
	/**
	 * ...
	 * @author bimkute
	 */
	public class Result extends Base 
	{
		private var timer:Timer;
		public function Result(win:Boolean) 
		{
			trace("GameData.getInstance().totalWin tai result", GameData.getInstance().totalWin)
			_shadow = new Sprite();
			_shadow.graphics.beginFill(0x000000, .3);
			_shadow.graphics.drawRect(0, 0, 1499, 900);
			_shadow.graphics.endFill();
			addChild(_shadow);
			addContent("popup", "playGame");
			content.x = (1499 - content.width) / 2;
			content.y = (900 - content.height) / 2 - 50;
			if (win && GameData.getInstance().totalWin > 0)
			{
				content.noticeTxt.text = "Chúc mừng bạn đã dành chiến thắng!";
				content.showResultTxt.text = "Bạn thắng với số tiền là: " + String(GameData.getInstance().totalWin);
			}
			else
			{
				content.noticeTxt.text = "Buồn quá bạn đã thua rồi! ";
				content.showResultTxt.text = "Hãy cố gắng ở lần sau nhé bạn!!!";
			}
			content.btnXacNhan.addEventListener(MouseEvent.CLICK, onClick);
			timer = new Timer(1000, 10);
			timer.addEventListener(TimerEvent.TIMER_COMPLETE, autoClose);
			timer.start();
		}
		
		private function autoClose(e:TimerEvent):void 
		{
			timer.stop();
			timer.removeEventListener(TimerEvent.TIMER_COMPLETE, autoClose);
			
			dispatchEvent(new Event("close"));
		}
		
		private function onClick(e:MouseEvent):void 
		{
			content.btnXacNhan.removeEventListener(MouseEvent.CLICK, onClick);
			dispatchEvent(new Event("close"));
		}
		
		public function removeAllEvent():void
		{
			if (content)
			{
				content.btnXacNhan.removeEventListener(MouseEvent.CLICK, onClick);
				removeChild(content);
				content = null;
			}
			if (_shadow)
			{
				removeChild(_shadow);
				_shadow = null;
			}
		}
	}

}